﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Masini
{
    class ObjectPool
    {
        static int _intMaxPoolSize = 2;
       
        public  Queue objPoolSedan = new Queue(_intMaxPoolSize);
        public  Queue objPoolCoupe = new Queue(_intMaxPoolSize);
        public Queue objPoolMini = new Queue(_intMaxPoolSize);



//Sedan
        public Sedan GetMasinaSedan()
        {
            Sedan objMasina;
            if (Sedan.nr >= 2 && objPoolSedan.Count > 1)
            {
                objMasina = GetMasinaSedanFromPool();
            }
            else
            {
                objMasina = CreateNewMasina();
            }
            return objMasina;
        }

        private Sedan GetMasinaSedanFromPool()
            {
                Sedan objMasina;
            if (objPoolSedan.Count > 1)
            {
                objMasina = (Sedan)objPoolSedan.Dequeue();
                Sedan.nr--;
            }
            else
            {
                objMasina = new Sedan();
                objMasina.setari();

            }
            return objMasina;
            }

        private Sedan CreateNewMasina()
        {
            Sedan objMasina = new Sedan();
            objMasina.setari();
        objPoolSedan.Enqueue(objMasina);
       
        Sedan.nr++;
        return objMasina;
        }

        
//Coupe
        public Coupe GetMasinaCoupe()
        {
            Coupe objMasina;
            if (Coupe.nr >= 2 && objPoolCoupe.Count > 1)
            {
                objMasina = GetMasinaCoupeFromPool();
            }
            else
            {
                objMasina = CreateNewMasina2();
            }
            return objMasina;
        }

        private Coupe GetMasinaCoupeFromPool()
        {
            Coupe objMasina;
            if (objPoolSedan.Count > 1)
            {
                objMasina = (Coupe)objPoolSedan.Dequeue();
                Coupe.nr--;
            }
            else
            {
                objMasina = new Coupe();
                objMasina.setari();
            }
            return objMasina;
        }

        private Coupe CreateNewMasina2()
        {
            Coupe objMasina = new Coupe();
            objMasina.setari();
            objPoolCoupe.Enqueue(objMasina);
            return objMasina;
        }


// Mini
        public Mini GetMasinaMini()
        {
            Mini objMasina;
            if (Mini.nr >= 2 && objPoolMini.Count > 1)
            {
                objMasina = GetMasinaMiniFromPool();
            }
            else
            {
                objMasina = CreateNewMasina3();
            }
            return objMasina;
        }

        private Mini GetMasinaMiniFromPool()
        {
            Mini objMasina;
            if (objPoolMini.Count > 1)
            {
                objMasina = (Mini)objPoolMini.Dequeue();
                Mini.nr--;
            }
            else
            {
                objMasina = new Mini();
                objMasina.setari();
            }
            return objMasina;
        }

        private Mini CreateNewMasina3()
        {
            Mini objMasina = new Mini();
            objMasina.setari();
            objPoolMini.Enqueue(objMasina);
            return objMasina;
        }

    }


    }

